﻿using ExpressesClass.Model;
using System;
using System.Drawing;
using System.Drawing.Printing;
using ZXing;
using ZXing.Common;

namespace ExpressesClass.IPrints
{
    public class StoPrint : IPrint
    {
        private Kddh kddh;
        private int printCur;
        private DateTime now;

        private int _w, _5, _8, _10, _12, _13, _16, _20, _25, _35, _50;

        public StoPrint(Kddh kddh, int printCur, DateTime now)
        {
            this.kddh = kddh;
            this.printCur = printCur;
            this.now = now;
            _w = 290;
            _5 = 20;
            _8 = 36;
            _10 = 40;
            _12 = 48;
            _13 = 52;
            _16 = 64;
            _20 = 80;
            _25 = 100;
            _35 = 140;
            _50 = 200;
        }

        public void Print(string printer)
        {
            PrintDocument printDoc = new PrintDocument();
            printDoc.PrinterSettings.PrinterName = printer;
            printDoc.DefaultPageSettings.PaperSize = new PaperSize("DepponSize", 290, 500);
            printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage); //打印事件 
            printDoc.Print();
        }
        private void printDoc_PrintPage(object sender, PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            //设置高质量插值法   
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度   
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            //消除锯齿
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            //打印时间
            PrintPrintTime(g, 5, _8, _35, _8);
            //快递类型
            PrintShipName(g, _50, 10, _35, _12);
            //横向面单号
            PrintHorMailNo(g, 5, _12 + 5, _w - _5, _13);
            //运单号
            PrintMailNo(g, +_16 + 10, _12 + _13 + 5, _w - _5, _13);
            //大笔
            PrintSortingCode(g, 10, _12 + _20, _w, _20);
            //集包地
            PrintPkgCode(g, 10, _12 + _20 + _8, _w, _20);
            //收
            PrintReceive(g, 5, _12 + _20 + _8 + _8 + _5, 28, 28);
            //收件人
            PrintReceiveMan(g, _8 + 5, _12 + _20 + _8 + _8 + 2, _w - _8, _5);
            //收件人地址
            PrintReceiveAddress(g, _8 + 5, _12 + _20 + _8 + _8 + _5 + 5, _w - _8, _10);
            //寄
            PrintSend(g, 5, _12 + _20 + _8 + _8 + _20 + _5, 26, 26);
            //寄件人
            PrintSendMan(g, _8 + 5, _12 + _20 + _8 + _8 + _20 + 2, _w, _8);
            //寄件人地址
            PrintSendAddress(g, _8 + 5, _12 + _20 + _8 + _8 + _20 + _5 + 5, _w, _8);
            //备注
            PrintRemark(g, 5, _12 + _20 + _8 + _8 + _20 + _20 + 2, _w - _5, _25);
            //已检视
            PrintLook(g, _50 + _5, _12 + _20 + _8 + _8 + _20 + _20 + _5, _w - _5, _10);
            //位置
            PrintWz(g, 5, _12 + _20 + _8 + _8 + _20 + _20 + _5, _w - _5, _8);
            //横向面单号
            PrintHorMailNo(g, 5, _12 + _20 + _8 + _8 + _20 + _20 + _12 + 5, _w - _5, _13);
            //运单号
            PrintMailNo(g, +_16 + 10, _12 + _20 + _8 + _8 + _20 + _20 + _12 + _13 + 5, _w - _5, _13);
            //边框
            PrintRectangle(g);
        }

        private void PrintRectangle(Graphics g)
        {
            Pen pen = new Pen(Brushes.Black, 1);
            pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
            //横
            g.DrawLine(pen, 0, _12, 280, _12);
            g.DrawLine(pen, 0, _12 + _20, 280, _12 + _20);
            g.DrawLine(pen, 0, _12 + _20 + _8, 280, _12 + _20 + _8);
            g.DrawLine(pen, 0, _12 + _20 + _8 + _8, 280, _12 + _20 + _8 + _8);
            g.DrawLine(pen, 0, _12 + _20 + _8 + _8 + _20, 280, _12 + _20 + _8 + _8 + _20);
            g.DrawLine(pen, 0, _12 + _20 + _8 + _8 + _20 + _20, 280, _12 + _20 + _8 + _8 + _20 + _20);
            g.DrawLine(pen, 0, _12 + _20 + _8 + _8 + _20 + _20 + _12, 280, _12 + _20 + _8 + _8 + _20 + _20 + _12);
            g.DrawLine(pen, 0, _12 + _20 + _8 + _8 + _20 + _20 + _12 + _20, 280, _12 + _20 + _8 + _8 + _20 + _20 + _12 + _20);
        }
        private void PrintPrintTime(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 6, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(now.ToString("yyyy/MM/dd HH:mm:ss"), font, Brushes.Black, rec);
        }
        private void PrintShipName(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 15, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.shipName, font, Brushes.Black, rec);
        }
        private void PrintHorMailNo(Graphics g, int x, int y, int w, int h)
        {
            EncodingOptions encodeOption = new EncodingOptions();
            encodeOption.Width = 100;
            encodeOption.Height = 30;
            BarcodeWriter wr = new BarcodeWriter();
            wr.Options = encodeOption;
            wr.Format = BarcodeFormat.CODE_128;
            encodeOption.PureBarcode = true;
            System.Threading.Thread.Sleep(100);

            string mailNo = kddh.mailNo;
            Bitmap img = wr.Write(mailNo);
            g.DrawImage(img, x, y, w, h);
        }
        private void PrintMailNo(Graphics g, int x, int y, int w, int h)
        {
            Font font = new Font("微软雅黑", 10, FontStyle.Bold);
            RectangleF recMailNo = new RectangleF(x, y, w, h);
            g.DrawString(kddh.mailNo, font, Brushes.Black, recMailNo);
        }
        private void PrintSortingCode(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 18, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.sortingCode, font, Brushes.Black, rec);
        }
        private void PrintPkgCode(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 18, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.pkgCode, font, Brushes.Black, rec);
        }
        private void PrintReceive(Graphics g, int x, int y, int w, int h)
        {
            Bitmap img = Resource.so;
            g.DrawImage(img, x, y, w, h);
        }
        private void PrintReceiveMan(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 13, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.receiveMan + " " + kddh.receiveManPhone, font, Brushes.Black, rec);
        }
        private void PrintReceiveAddress(Graphics g, float x, float y, float w, float h)
        {
            Font font = new Font("微软雅黑", 10, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.receiveManAddress, font, Brushes.Black, rec);
        }
        private void PrintSend(Graphics g, int x, int y, int w, int h)
        {
            Bitmap img = Resource.ji;
            g.DrawImage(img, x, y, w, h);
        }
        private void PrintSendMan(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 10, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.sendMan + " " + kddh.sendManPhone, font, Brushes.Black, rec);
        }
        private void PrintSendAddress(Graphics g, float x, float y, float w, float h)
        {
            Font font = new Font("微软雅黑", 10, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.sendManAddress, font, Brushes.Black, rec);
        }
        private void PrintLook(Graphics g, float x, float y, float w, float h)
        {
            Font font = new Font("微软雅黑", 10, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString("已检视", font, Brushes.Black, rec);
        }
        private void PrintRemark(Graphics g, float x, float y, float w, float h)
        {
            string str = kddh.remark;

            Font font = new Font("微软雅黑", 8, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString("序号:" + printCur + ";" + str, font, Brushes.Black, rec);
        }
        private void PrintWz(Graphics g, float x, float y, float w, float h)
        {
            string str = kddh.wzh;

            Font font = new Font("微软雅黑", 16, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(str, font, Brushes.Black, rec);
        }
    }
}
